package com.clkj.zhonghui.pojo.common;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author yawang
 * @create 2024/07/05 08:53:48
 **/
public class ImgUtils {



    public static String  graphicsGeneration(String imgurl, String imgurl1,
                                          String imgPic) throws IOException {
        try {
            // 加载源图片1
            BufferedImage image1 = ImageIO.read(new File(imgurl));
            int width1 = image1.getWidth();
            int height1 = image1.getHeight();

            // 加载源图片2
            BufferedImage image2 = ImageIO.read(new File(imgurl1));
            int width2 = image2.getWidth();
            int height2 = image2.getHeight();

            // 创建Graphics2D对象，用在底图对象上绘图
            Graphics2D g2d = image2.createGraphics();
            int waterImgWidth = width1;// 获取层图的宽度
            int waterImgHeight = height1;// 获取层图的高度
            // 在图形和图像中实现混合和透明效果
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1));
            g2d.setComposite(AlphaComposite.SrcOver);
//
            int width = (width2-width1)/2;
            int height =(height2-145-195);            // 绘制
            g2d.drawImage(image1, width, height, waterImgWidth, waterImgHeight, null);
            g2d.dispose();// 释放图形上下文使用的系统资源
            BufferedImage targetImage = image2;
            // 保存目标图片
            ImageIO.write(targetImage, "png", new File(imgPic));

            System.out.println("图片合成成功！");
            return imgPic;
        } catch (Exception e) {
            System.out.println("图片合成失败：" + e.getMessage());
            return "error";
        }
    }
}
